package com.ham.mud.rooms;

import com.ham.mud.Colors;
import com.ham.mud.ServerConnection;
import com.ham.mud.characters.mobs.Mob;
import com.ham.mud.characters.player.Player;
import com.ham.mud.items.Item;
import com.ham.mud.quests.KillQuest;
import com.ham.mud.quests.Quest;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by hlucas on Jun 16, 2011 at 2:04:08 PM
 */
public class Room {
    String zone;
    int atX;
    int atY;

    List<ServerConnection> players = new ArrayList<ServerConnection>();
    String name;
    String description;
    String[] exits;
    List<Mob> mobs;
    List<Item> items;
    String ascii;
    RoomType type;

    public Room(String zone, int atX, int atY, String name, String description, String[] exits, List<Mob> mobs, List<Item> items, String ascii, RoomType type) {
        this.zone = zone;
        this.atX = atX;
        this.atY = atY;
        this.name = name;
        this.description = description;
        this.exits = exits;
        this.mobs = mobs;
        this.items = items;
        this.ascii = ascii;
        this.type = type;
    }

    public List<ServerConnection> getOtherConnections(ServerConnection serverConnection) {
        List<ServerConnection> others = new ArrayList<ServerConnection>();
        others.addAll(players);
        String playerName = serverConnection.getPlayer().getName();
        Iterator<ServerConnection> it = others.iterator();
        while(it.hasNext()) {
            ServerConnection next = it.next();
            if(next.getPlayer().getName().equals(playerName)) {
                it.remove();
                break;
            }
        }
        return others;
    }

    public List<Player> getOtherPlayers(ServerConnection connection) {
        List<ServerConnection> conns = getOtherConnections(connection);
        List<Player> players = new ArrayList<Player>();
        for(ServerConnection conn : conns) {
            players.add(conn.getPlayer());
        }
        return players;
    }

    public void printToOthers(String string, ServerConnection serverConnection) {
        List<ServerConnection> connections =  getOtherConnections(serverConnection);
        for(ServerConnection connection : connections) {
            connection.printSolo(Colors.NEW_LINE + string);
        }
    }

    public void addPlayer(ServerConnection connection, String cameFrom) {
        if(cameFrom != null) {
            if(cameFrom.equals("poof")) {
                this.printToOthers(connection.getPlayer().getName() + " appears in a poof of smoke.", connection);
            } else {
                this.printToOthers(connection.getPlayer().getName() + " has arrived from the " + cameFrom + ".", connection);
            }
        }
        players.add(connection);
    }

    public void removePlayer(ServerConnection connection, String leftTo) {
        if(leftTo != null) {
            this.printToOthers(connection.getPlayer().getName() + " has left to the " + leftTo + ".", connection);
        } else {
            this.printToOthers(connection.getPlayer().getName() + " disappears in a poof of smoke.", connection);
        }
        players.remove(connection);
    }

    public String getZone() {
        return zone;
    }

    public void setZone(String zone) {
        this.zone = zone;
    }

    public int getAtX() {
        return atX;
    }

    public void setAtX(int atX) {
        this.atX = atX;
    }

    public int getAtY() {
        return atY;
    }

    public void setAtY(int atY) {
        this.atY = atY;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String[] getExits() {
        return exits;
    }

    public void setExits(String[] exits) {
        this.exits = exits;
    }

    public List<Mob> getMobs() {
        return mobs;
    }

    public void setMobs(List<Mob> mobs) {
        this.mobs = mobs;
    }

    public void addMob(Mob mob) {
        if(mobs == null) mobs = new ArrayList<Mob>();
        mobs.add(mob);
    }

    public void removeMob(String mobName) {
        if(mobs == null) mobs = new ArrayList<Mob>();
        Iterator<Mob> it = mobs.iterator();
        while(it.hasNext()) {
            Mob mob = it.next();
            if(mob.getFileName().equals(mobName)) {
                it.remove();
                break;
            }
        }
    }

    public void removeMob(Mob combatWith) {
        mobs.remove(combatWith);
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    public void removeItem(Item item) {
        if(item == null) return;
        items.remove(item);
    }

    public void addItem(Item item) {
        if(item == null) return;
        items.add(item);
    }

    public String getAscii() {
        return ascii;
    }

    public void setAscii(String ascii) {
        this.ascii = ascii;
    }

    public RoomType getType() {
        return type;
    }

    public void setType(RoomType type) {
        this.type = type;
    }

    public Exit getExit(int x, int y) {
        String seeking = RoomService.getExitString(x, y);
        for(String s : exits) {
            String[] split = s.split(":");
            s = split[0];
            if(s.equals(seeking)) {
                if(split.length == 1) {
                    return new Exit();
                } else {
                    return new Exit(split[1], Integer.valueOf(split[2]), Integer.valueOf(split[3]));
                }
            }
        }

        return null;
    }

    public void display(ServerConnection connection) {
        connection.println();

        if(ascii != null) {
            Colors.printASCII(zone, ascii, connection);
        }

        connection.println(name + Colors.NEW_LINE);
        connection.println("    " + description + Colors.NEW_LINE);

        Mob questMob = null;
        Quest quest = connection.getPlayer().getQuest();
        if(quest != null && quest instanceof KillQuest && !quest.isComplete(connection.getPlayer())) {
            questMob = ((KillQuest)quest).getMob();
        }

        displayExits(connection);

        if(mobs.size() > 0) {
            connection.println();
        }

        for(Mob mob : mobs) {
            if(questMob != null && mob.equals(questMob)) {
                connection.println("    " + mob.getShortDescription() + " [QUEST TARGET]");
            } else {
                connection.println("    " + mob.getShortDescription());
            }
        }

        List<ServerConnection> others = getOtherConnections(connection);
        for(ServerConnection other : others) {
            connection.println("    " + other.getPlayer().getShortDescription());
        }

        if(mobs.size() == 0 && items.size() > 0) {
            connection.println();
        }

        for(Item item : items) {
            connection.println("    " + item.getShortDescription());
        }

        connection.printEnd();
    }

    public void displayExits(ServerConnection connection) {
        StringBuilder exitString = new StringBuilder();
        exitString.append("[exits: ");
        if(exits.length == 0) {
            exitString.append("none");
        }
        for(int i = 0; i < exits.length; i++) {
            String exit = exits[i].split(":")[0];
            exitString.append(exit);
            if(i != exits.length - 1) {
                exitString.append(", ");
            }
        }
        exitString.append("]");
        connection.println(exitString.toString());
    }
}
